﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.SaveData;
using MainGame.GamePlay;
using System.Diagnostics;

namespace MainGame.Menu
{
  

  public interface IGraphicsSettings
  {
 
  }

  public class GraphicsSettings : BaseMenu, IGraphicsSettings
  {

    Button ChangeBg, SelectedBg, ChangeDeck, SelectedDeck, SelectedBDeck, square;
    Color color;
    public bool Animated = false, backClicked = false, SquareOn = false;
    public int Direction = 1, alphaChange = 1, AnimVelocity = 160;
    Backgrounds currentBackground;
    Decks currentDeck;
    SpriteBatch spriteBatch;


    public GraphicsSettings(Game game, MenuDepths depth)
      : base(game, depth)
    {
      background = null;
      bounds = new Rectangle(game.GraphicsDevice.Viewport.Bounds.Center.X, 40, 220, 330);
      //game.Services.AddService(typeof(IGraphicsSettings), this);
    }

    protected override void Dispose(bool disposing)
    {
      //Game.Services.RemoveService(typeof(IGraphicsSettings));
      base.Dispose(disposing);
    }

    public override void Initialize()
    {

      color = Color.Yellow;
      color.R = 150;

      spriteBatch = Game.GetContext().SpriteBatch;

      //load saved graphics configuration

      //load saved background configuration
      currentBackground = Game.GetContext().currentConfig.chosenBackground;
     
      //load saved deck configuration
      currentDeck = Game.GetContext().currentConfig.chosenDeck;
      
      // Load all buttons
      square = new Button(Game, @"Images//Menu//square", new Rectangle(Game.GraphicsDevice.Viewport.Bounds.Center.X + 100, bounds.Y + 15, 160, 160), Color.Transparent, "small_font", "", Color.Blue);
      AddButton(square, () =>
      {
      });


      ChangeBg = new Button(Game, @"Images//Menu//button2", new Rectangle(Game.GraphicsDevice.Viewport.Bounds.Center.X - 10, (int)bounds.Y + 180, 140, 100), Color.Green, "baveuse1_font", Locale.Strings.ChangeBackground.Replace("\\n", "\n"), Color.Blue, 0.8f);
      AddButton(ChangeBg, () =>
      {
        changeBackground();
        
        SquareOn = true;
        square.bounds.X = SelectedBg.bounds.X - 10;
        square.bounds.Y = SelectedBg.bounds.Y - 10;
        square.bounds.Width = SelectedBg.bounds.Width + 20;
        square.bounds.Height = SelectedBg.bounds.Height + 20;

        Game.GetContext().currentConfig.chosenBackground = currentBackground;
        
      });

      SelectedBg = new Button(Game, @"Images//Backgrounds//Thumbs//Bg_" + currentBackground.ToString(), new Rectangle(Game.GraphicsDevice.Viewport.Bounds.Center.X + 190, (int)bounds.Y + 180, 140, 140), Color.White, "small_font", "", Color.Blue);
      AddButton(SelectedBg, null);



      ChangeDeck = new Button(Game, @"Images//Menu//button2", new Rectangle(Game.GraphicsDevice.Viewport.Bounds.Center.X - 10, (int)bounds.Y + 50, 140, 100), Color.Green, "baveuse1_font", Locale.Strings.ChangeDeck.Replace("\\n", "\n"), Color.Blue, 0.8f);
      AddButton(ChangeDeck, () =>
      {
        changeDeck();
        SquareOn = true;
        square.bounds.X = SelectedDeck.bounds.X - 10;
        square.bounds.Y = SelectedDeck.bounds.Y - 10;
        square.bounds.Width = SelectedDeck.bounds.Width + 20;
        square.bounds.Height = SelectedDeck.bounds.Height + 20;

        Game.GetContext().currentConfig.chosenDeck = currentDeck;
      });

      SelectedDeck = new Button(Game, @"Images//Decks//Deck_" + currentDeck.ToString() + "_mini", new Rectangle(Game.GraphicsDevice.Viewport.Bounds.Center.X + 190, (int)bounds.Y + 50, 145, 90), Color.White, "small_font", "", Color.Blue);
      AddButton(SelectedDeck, () => { });

      //SelectedBDeck = new Button(Game, @"Images//Decks//Deck_Big_mini2" /*@"Images//Decks//Deck_" + currentDeck.ToString() + "_mini"*/, new Rectangle(Game.GraphicsDevice.Viewport.Bounds.Center.X + 265, (int)bounds.Y + 50, 70, 90), Color.White, "small_font", "", Color.Blue);
      //AddButton(SelectedBDeck, () => { });

      base.Initialize();
    }


    private void changeBackground()
    {
      
      if (currentBackground < Backgrounds.NumBackgrounds - 1)
        currentBackground++;
      else
        currentBackground = 0;
         
      SelectedBg.texture = Game.Content.Load<Texture2D>(@"Images//Backgrounds//Thumbs//Bg_" + currentBackground.ToString());         

    }

    private void changeDeck()
    {

      if (currentDeck < Decks.NumDecks - 1)
        currentDeck++;
      else
        currentDeck = 0;
      
      SelectedDeck.texture = Game.Content.Load<Texture2D>(@"Images//Decks//Deck_" + currentDeck.ToString()+"_mini");

    }


    public override void Update(GameTime gameTime)
    {

      if (SquareOn)
      {
        color.R += (byte)(alphaChange * 10);

        if ((color.R >= 240) || (color.R <= 10))
          alphaChange *= -1;

        square.color = color; 
      }

      if (backClicked)
      {
        foreach (Button b in (from cb in buttons select cb.b))
        {
          b.bounds.X += AnimVelocity - 60;
        }

      }
      
      base.Update(gameTime);
    }


    public override void Draw(GameTime gameTime)
    {
      spriteBatch.Begin();

      if (!backClicked)
      {
        spriteBatch.Draw(Game.Content.Load<Texture2D>(@"Images//Game//roundedBox"), new Rectangle(Game.GraphicsDevice.Viewport.Bounds.Center.X + 180, (int)bounds.Y + 170, 160, 160), new Color(40, 100, 10, 60));
        spriteBatch.Draw(Game.Content.Load<Texture2D>(@"Images//Game//roundedBox"), new Rectangle(Game.GraphicsDevice.Viewport.Bounds.Center.X + 180, (int)bounds.Y + 40, 165, 110), new Color(40, 100, 10, 60));
      }

      spriteBatch.End();
     

      base.Draw(gameTime);
            
      spriteBatch.Begin();
            
      spriteBatch.Draw(Game.Content.Load<Texture2D>(@"Images//Game//roundedBox"), new Rectangle(Game.GraphicsDevice.Viewport.Bounds.Center.X + 180, (int)bounds.Y + 170, 160, 160), new Color(0, 0, 0, 30));
       
      spriteBatch.End();
    }

  }
}
